﻿using Masuit.Tools.Security;

namespace SimpleX
{
    public class PwdUtils
    {
        private static readonly string _salt = "SLTSPXMX";

        public static string DefaultPwd()
        {
            return "123456".MDString3(_salt);
        }

        public static string ToPwd(string @string)
        {
            return @string.MDString3(_salt);
        }

        /// <summary>
        /// 密码相似度
        /// </summary>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public static double Similarity(string oldPassword, string newPassword)
        {
            int editDistance = LevenshteinDistance(oldPassword, newPassword);
            double similarity = 1.0 - ((double)editDistance / (double)Math.Max(oldPassword.Length, newPassword.Length));
            return similarity * 100;
        }

        /// <summary>
        /// 计算莱文斯坦距离算法
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        public static int LevenshteinDistance(string s1, string s2)
        {
            int[,] distance = new int[s1.Length + 1, s2.Length + 1];

            for (int i = 0; i <= s1.Length; i++)
                distance[i, 0] = i;

            for (int j = 0; j <= s2.Length; j++)
                distance[0, j] = j;

            for (int i = 1; i <= s1.Length; i++)
            {
                for (int j = 1; j <= s2.Length; j++)
                {
                    int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;

                    distance[i, j] = Math.Min(
                        Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1),
                        distance[i - 1, j - 1] + cost);
                }
            }
            return distance[s1.Length, s2.Length];
        }
    }
}